package com.groupbuying.store.service.impl;

import com.groupbuying.store.constants.CommonConstants;
import com.groupbuying.store.dao.StoreAccountRoleRelDao;
import com.groupbuying.store.domain.StoreAccountRoleRelDO;
import com.groupbuying.store.service.StoreAccountRoleRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StoreAccountRoleRelServiceImpl implements StoreAccountRoleRelService {
    @Autowired
    private StoreAccountRoleRelDao storeAccountRoleRelDao;

    @Override
    public StoreAccountRoleRelDO get(String uuid) {
        return storeAccountRoleRelDao.get(uuid);
    }

    @Override
    public List<StoreAccountRoleRelDO> list(Map<String, Object> map) {
        return storeAccountRoleRelDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return storeAccountRoleRelDao.count(map);
    }

    @Override
    public int save(StoreAccountRoleRelDO storeAccountRoleRel) {
        return storeAccountRoleRelDao.save(storeAccountRoleRel);
    }

    @Override
    public int update(StoreAccountRoleRelDO storeAccountRoleRel) {
        return storeAccountRoleRelDao.update(storeAccountRoleRel);
    }

    @Override
    public int remove(String uuid) {
        return storeAccountRoleRelDao.remove(uuid);
    }

    @Override
    public int batchRemove(String[] uuids) {
        return storeAccountRoleRelDao.batchRemove(uuids);
    }

    @Override
    public void deleteRoleRelByAccountUuid(String staffUuid) {

        Map map = new HashMap();
        map.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        map.put("storeaccountuuid", staffUuid);
        List<StoreAccountRoleRelDO> list = storeAccountRoleRelDao.list(map);
        if (list != null && list.size() > 0) {
            List<String> uuids = new ArrayList<String>();
            for (StoreAccountRoleRelDO m : list) {
                uuids.add(m.getUuid());
            }
            storeAccountRoleRelDao.batchRemove(uuids.toArray(new String[uuids.size()]));
        }
    }

    @Override
    public void batchCreate(List<StoreAccountRoleRelDO> accountrelrolelist) {
        storeAccountRoleRelDao.batchCreate(accountrelrolelist);
    }

    @Override
    public List<String> getRoleUuidListByStoreAccountUuid(String storeAccountUuid) {

        Map map = new HashMap();
        map.put("delflag",CommonConstants.COMMON_FLAG_UNDEL);
        map.put("storeaccountuuid", storeAccountUuid);
        //1.通过AccountUUID查询所有的关联关系
        List<StoreAccountRoleRelDO> relList = this.list(map);
        if (relList == null || relList.size() == 0) {
            return null;
        }
        //2.遍历获取所有的RoleUuid
        List<String> roleUuids = new ArrayList<String>();
        for (StoreAccountRoleRelDO rel : relList) {
            roleUuids.add(rel.getStoreroleuuid());
        }
        return roleUuids;
    }

}
